﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ProcessoSeletivo.DAO
{
    public class Gabarito : IDisposable
    {
        #region " Listar "

        public DataView ListarGabaritoPorInscricao(int cod_inscricao_ins)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ListarGabaritoPorInscricao", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_inscricao_ins", cod_inscricao_ins);

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_gabarito");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        public DataView ListarGabaritoPorProcesso(int cod_processo_pro)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ListarGabaritoPorProcesso", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_processo_pro", cod_processo_pro);

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_gabarito");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }
        
        public DataView ListarGabaritoPorFiltro(string des_processo_pro, int tip_processo_pro)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ListarGabaritoPorFiltro", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                if (des_processo_pro != "-1")
                {
                    sqlc.Parameters.AddWithValue("@des_processo_pro", des_processo_pro);
                }
                else
                {
                    sqlc.Parameters.AddWithValue("@des_processo_pro", DBNull.Value);
                }

                if (tip_processo_pro > -1)
                {
                    sqlc.Parameters.AddWithValue("@tip_processo_pro", tip_processo_pro);
                }
                else
                {
                    sqlc.Parameters.AddWithValue("@tip_processo_pro", DBNull.Value);
                }

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_gabarito");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        #endregion

        #region " Incluir "

        public void IncluirGabarito(int cod_processo_pro, string des_login_usu)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("IncluirGabarito", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_processo_pro", cod_processo_pro);
                sqlc.Parameters.AddWithValue("@des_login_usu", des_login_usu);
                sqlc.ExecuteNonQuery();
                sql.Close();
            }
        }

        #endregion

        #region " Alterar "

        public void AlterarGabarito(int cod_processo_pro, string des_login_usu)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("AlterarGabarito", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_processo_pro", cod_processo_pro);
                sqlc.Parameters.AddWithValue("@des_login_usu", des_login_usu);
                sqlc.ExecuteNonQuery();
                sql.Close();
            }
        }

        #endregion

        #region " Excluir "

        public void ExcluirGabarito(int cod_processo_pro)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ExcluirGabarito", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_processo_pro", cod_processo_pro);
                sqlc.ExecuteNonQuery();
                sql.Close();
            }
        }

        #endregion

        #region " Dipose "

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
